//
//  QXSendNewVC.swift
//  QXWeibo
//
//  Created by QinXin on 15/7/9.
//  Copyright © 2015年 labi3285_Lab. All rights reserved.
//

import UIKit
import SVProgressHUD
import FFAutoLayout

class QXSendNewVC: UIViewController, UITextViewDelegate {
    
    lazy var inputBox = UITextView()
    lazy var placeHolderLabel = UILabel()
    lazy var toolBar = UIToolbar()
    private lazy var emoticonKeyboardVC: QXEmoticonKeyboardVC = QXEmoticonKeyboardVC { [unowned self] (emoticon) -> () in
        self.inputBox.insertEmoticon(emoticon)
    }
    
    class func create() -> UINavigationController {
        let vc = QXSendNewVC()
        return UINavigationController(rootViewController: vc)
    }
    
    override func loadView() {
        view = UIView()
        view.backgroundColor = UIColor.whiteColor()
        addChildViewController(emoticonKeyboardVC)
        setupNavigationBar()
        setupInputBox()
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        inputBox.becomeFirstResponder()
    }
    
    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)
        inputBox.resignFirstResponder()
    }
    
    private func setupToolBar() {

        toolBar.backgroundColor = UIColor(white: 0.8, alpha: 1.0)
        
        let itemSettings = [["imageName": "compose_toolbar_picture", "action": "selectPicture"],
            ["imageName": "compose_mentionbutton_background"],
            ["imageName": "compose_trendbutton_background"],
            ["imageName": "compose_emoticonbutton_background", "action": "inputEmoticon"],
            ["imageName": "compose_addbutton_background"]]
        
        // 所有按钮的数组
        var items = [UIBarButtonItem]()
        
        // 设置按钮 - 问题：图片名称(高亮图片) / 监听方法
        for settings in itemSettings {
            items.append(QXBarButtonItem(imageName: settings["imageName"]!, highlightedImageName: nil, target: nil, actionName: settings["action"]))
            
            items.append(UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil))
        }
        // 删除末尾的弹簧
        items.removeLast()
        toolBar.items = items
        
        view.addSubview(toolBar)
        
        toolBar.ff_AlignInner(ff_AlignType.BottomLeft, referView: view, size: nil)
        
    }
    
    private func setupNavigationBar() {
        // 1. 左侧按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle.Plain, target: self, action: "close")
        
        // 2. 右侧按钮
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "发送", style: UIBarButtonItemStyle.Plain, target: self, action: "sendStatus")
        navigationItem.rightBarButtonItem?.enabled = false
        
        // 3. 导航栏
        let titleView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 32))
        let label1 = UILabel(color: UIColor.darkGrayColor(), fontSize: 15)
        label1.text = "发微博"
        label1.sizeToFit()
        let label2 = UILabel(color: UIColor(white: 0.0, alpha: 0.5), fontSize: 13)
        label2.text = sharedAccount?.name ?? ""
        label2.sizeToFit()
        
        titleView.addSubview(label1)
        titleView.addSubview(label2)
        
        label1.ff_AlignInner(ff_AlignType.TopCenter, referView: titleView, size: nil)
        label2.ff_AlignInner(ff_AlignType.BottomCenter, referView: titleView, size: nil)
        
        navigationItem.titleView = titleView
    }
    
    private func setupInputBox() {
        view.addSubview(inputBox)
        
        inputBox.font = UIFont.systemFontOfSize(18)
        inputBox.ff_Fill(view)
        
//        inputBox.delegate = self
        
        // 设置输入视图的辅助视图
//        inputBox.inputAccessoryView = createToolBar()
        
        // 设置垂直滚动
        inputBox.alwaysBounceVertical = true
        // 滚动关闭键盘
        inputBox.keyboardDismissMode = UIScrollViewKeyboardDismissMode.OnDrag
        
        // 设置 占位标签
        placeHolderLabel.text = "分享新鲜事..."
        placeHolderLabel.textColor = UIColor.lightGrayColor()
        placeHolderLabel.sizeToFit()
        inputBox.addSubview(placeHolderLabel)
        placeHolderLabel.ff_AlignInner(ff_AlignType.TopLeft, referView: inputBox, size: nil, offset: CGPoint(x: 5, y: 8))
    }

    ///  发布微博
    func sendStatus() {
        
        print("发微博")
    }
    
    ///  选择照片
    func selectPicture() {
        print("选择照片")
    }
    
    ///  关闭
    func close() {
        SVProgressHUD.dismiss()
        dismissViewControllerAnimated(true, completion: nil)
    }
    

}
